// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const uniID = require('uni-id-common')
const utils = require('uni-utils')
module.exports = {
	_before: function () { // 通用预处理器
		const methodName = this.getMethodName()
	   if(['get', 'add', 'update', 'delete'].includes(methodName) && !this.getUniIdToken()) {
	   	throw new Error('token不存在')
	   }
	   const clientInfo = this.getClientInfo()
	   this.uniIDIns = uniID.createInstance({ // 创建uni-id实例，其上方法同uniID
	   	clientInfo
	   })
	   this.unicloud = uniCloud.database()
	},
	get() {
		return new Promise((resolve, reject) => {
			let flag = false
			let uid = null
			utils.isCheckToken(this.uniIDIns, this.getUniIdToken()).then(res => {
				uid = res.uid
				const { startTime, endTime } = utils.getTodayTimestamps()
				const dateValue = this.unicloud.command.gte(startTime).and(this.unicloud.command.lte(endTime))
				return this.unicloud.collection('sign_in').where({ userId: res.uid, date: dateValue }).get()
			}).then(res => {
				flag = res.data.length > 0
				return this.unicloud.collection('sign_in').where({ userId: uid }).get()
			}).then(res => {
				resolve({ code: 200, data: res.data, total: res.data.length, result: flag, msg: '操作成功' })
			}).catch(c => utils.captureCatch(c, reject))
		})
	},
	/**
	 * 添加签到天数
	 * @param { string } userId 
	 * @param { string } ip 
	 * @param { number } date 
	*/
	add(data) {
		return new Promise((resolve, reject) => {
			const params = Object.assign({}, data)
			utils.isCheckToken(this.uniIDIns, this.getUniIdToken()).then(res => {
				params.createTime = utils.parseTime(new Date())
				return this.unicloud.collection('sign_in').add(params)
			}).then(res => {
				resolve({ code: 200, msg: '操作成功' })
			}).catch(c => utils.captureCatch(c, reject))
		})
	},
	/**
	 * 更新签到天数
	 * @param { string } id 
	 * @param { string } userId 
	 * @param { string } ip 
	 * @param { number } date 
	*/
	update(data) {
		return new Promise((resolve, reject) => {
			const params = Object.assign({}, data)
			utils.isCheckToken(this.uniIDIns, this.getUniIdToken()).then(res => {
				return this.unicloud.collection('sign_in').where({ _id: data.id }).update(params)
			}).then(res => {
				resolve({ code: 200, msg: '操作成功' })
			}).catch(c => utils.captureCatch(c, reject))
		})
	},
	/**
	 * 删除签到天数
	 * @param { string } _id 
	*/
	update(_id) {
		return new Promise((resolve, reject) => {
			const params = Object.assign({}, data)
			utils.isCheckToken(this.uniIDIns, this.getUniIdToken()).then(res => {
				return this.unicloud.collection('sign_in').where({ _id }).remove()
			}).then(res => {
				resolve({ code: 200, msg: '操作成功' })
			}).catch(c => utils.captureCatch(c, reject))
		})
	}
	/**
	 * method1方法描述
	 * @param {string} param1 参数1描述
	 * @returns {object} 返回值描述
	 */
	/* 
	method1(param1) {
		// 参数校验，如无参数则不需要
		if (!param1) {
			return {
				errCode: 'PARAM_IS_NULL',
				errMsg: '参数不能为空'
			}
		}
		// 业务逻辑
		
		// 返回结果
		return {
			param1 //请根据实际需要返回值
		}
	}
	*/
}
